package com.mm.base.quartz.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.mm.domain.common.DCommonCode;
import com.mm.domain.common.DCornQuartz;
import com.mm.mapper.note.DCornQuartzMapper;
import com.mm.serivce.common.IDCommonCodeService;
import com.mm.serivce.note.DNoteService;
import com.mm.serivce.quartz.MailService;
import com.mm.serivce.quartz.ShaDiaService;
import com.mm.util.quartz.CronUtil;
import com.mm.util.quartz.SchedulerJobUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import java.util.Date;
import java.util.Objects;

/**
 * 描述: 获取第三方语录发送邮件
 *
 * @author: yanglin
 * @Date: 2021-07-16-9:09
 * @Version: 1.0
 */
@Slf4j
@Component
public class ShaDiaClientTask {

    private final ShaDiaService shaDiaService;

    private final DNoteService noteService;

    private final MailService mailService;

    private final IDCommonCodeService codeService;

    private final SchedulerJobUtil schedulerAllJob;

    private final DCornQuartzMapper quartzMapper;

    private static String[] receiverNames = new String[]{"yling_mumu@163.com", "hhm_haime@163.com"};

    private static String[] wordPressNames = new String[]{"yling_mumu@163.com", "yling_muyi@163.com"};

    public ShaDiaClientTask(ShaDiaService shaDiaService, DNoteService noteService, MailService mailService, IDCommonCodeService codeService, SchedulerJobUtil schedulerAllJob, DCornQuartzMapper quartzMapper) {
        this.shaDiaService = shaDiaService;
        this.noteService = noteService;
        this.mailService = mailService;
        this.codeService = codeService;
        this.schedulerAllJob = schedulerAllJob;
        this.quartzMapper = quartzMapper;
    }


    /**
     * 获取语录发送邮件
     * 每天早上八点 @Scheduled(cron = "0/60 * * * * ? ")
     *
     * @throws InterruptedException
     * @Scheduled(cron = "10 39 14 2 12 ?")
     */
    //@Scheduled(cron = "2 48 13 28 3 ?")
    @Scheduled(cron = "0 0 8 * * ?")
    public void sendRainbowDay() throws MessagingException {
        log.info("定时器sendRainbowDay 执行开始....");
        DCommonCode commonCode = codeService.getCommonCodeByCode(10004);
        if (Objects.nonNull(commonCode) && StrUtil.isNotBlank(commonCode.getContent())) {
            receiverNames = commonCode.getContent().split(",");
        }
        // 主题
        String subject = "每日语录-" + DateUtil.format(new Date(), "yyyy-MM-dd");
        // 发送内容
        String content = shaDiaService.querySdAna();
        mailService.sendHtmlMail(subject, content, receiverNames);
        log.info("定时器sendRainbowDay 执行结束....");
    }

    /**
     * 每周五晚十点发送周报
     *
     * @throws InterruptedException
     * @Scheduled(cron = "0 0 22 ? * FRI")
     * 每周天晚十点发送周报
     * @Scheduled(cron = "0 0 22 ? * SUN")
     * SUN, MON, TUE, WED, THU, FRI and SAT
     */
    //@Scheduled(cron = "0 0 22 ? * SUN")
    public void sendWorkWeek() throws MessagingException {
        log.info("定时器sendWorkWeek 执行开始....");
        DCommonCode commonCode = codeService.getCommonCodeByCode(10005);
        if (Objects.nonNull(commonCode) && StrUtil.isNotBlank(commonCode.getContent())) {
            receiverNames = commonCode.getContent().split(",");
        }
        // 主题
        // 指定日期在第几周
        String subject = DateUtil.format(new Date(), "yyyy年MM月") + "[第" + DateUtil.weekOfMonth(new Date()) + "周周报]";
        // 发送内容
        String content = noteService.queryMyNoteWeek();

        mailService.sendHtmlMail(subject, content, receiverNames);
        log.info("定时器sendWorkWeek 执行结束....");
    }

    //@Scheduled(cron = "10 40 16 27 7 ?")
    //@Scheduled(cron = "0/60 * * * * ? ")
    public void quartzRun() throws SchedulerException {
        log.info("定时器quartzRun 执行开始....");
        // 判断什么时候触发
        String key = String.valueOf(System.currentTimeMillis());
        key = key.substring(key.length() - 3, key.length());

        DCornQuartz task = null;
        for (int i = 0; i < 5; i++) {
            task = new DCornQuartz();
            task.setCornId(System.currentTimeMillis());
            // 1:常规任务 2:特殊任务
            task.setType(1);
            task.setJobName("每日一乐-" + key);
            task.setJobGroup("quartz1-" + key);
            task.setQuartzTime(CronUtil.createCron(DateUtil.offset(DateUtil.date(), DateField.MINUTE, 1)));
            // 1:待执行 2:已执行
            task.setStatus(1);
            schedulerAllJob.addQuartz(task);
            quartzMapper.insert(task);
        }
        log.info("定时器quartzRun 执行结束....");
    }

}
